// pages/answer/QandA/QandA.js
var app = getApp();
//引用数据库   
const db = wx.cloud.database();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    grade: 0, //成绩
    questionnum: 10, //总数
    index: 0, //下标
    canAnswer: '下一题', //下一题或者完成答题
    isFinish:true,   //是否启用结束答题按钮
    passscore: 80, //80 通过
    rightAnswer: '___',
    answertimes: 0, //答题次数

    net: [],  //保存问题和答案
    answerIndex:[],   //保存哪个问题已经回答了
    passtimes: 0, //通过次数
    topscore: 0,
    isDisabled:false,  //为false时按钮可用，true时按钮禁用
    isShowAnswer:'none',   //是否显示答案
    isNextShow: 'none', //显示和隐藏下一题按钮

    checkimg:"https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/daquan/icons/rightanswer.png?sign=191df3f45a5d2ba44652034ea05cf3fc&t=1592142439",
    answercolor:"#9c3"
  },
  //选项点击操作
  Select: function(e) {
    //获取点击元素的id
    var select = e.currentTarget.id;
    select = select.toString();
    //获取点击问题的正确答案
    var right_answer = this.data.net[this.data.index].right_answer;
    right_answer = right_answer.toString();

    //答案正确
    if (right_answer.indexOf(select) != -1) {
      this.setData({
        answercolor:"#9c3",
        checkimg:"https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/daquan/icons/rightanswer.png?sign=191df3f45a5d2ba44652034ea05cf3fc&t=1592142439",
        grade: this.data.grade + (100 / this.data.questionnum), //得分+10
      })
    }else{
      this.setData({
        answercolor:"#d81e06",
        checkimg:"https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/daquan/icons/wronganswer.png?sign=4b8371136b32e5fb63e502b6fd571aec&t=1592142225"
      })
    }
    this.setData({
      rightAnswer: right_answer,
      isShowAnswer:'flex',
      isDisabled:true,   //选项按钮禁用
      isNextShow: 'inline-block',
    })
    //console.log(this.data.index)
    this.data.answerIndex.push(this.data.index)  //添加下标值到数组answerIndex中，表示已经答题
  },
  setAnswer(){

  },
  changeQuestion(e){
    console.log(e)
    if(e.detail.type=="next"){
      this.toNext();
    }else{
      this.toPrev();
    }
   
    let answerIndex=this.data.answerIndex;
    let index=this.data.index
    if(answerIndex.indexOf(index)!=-1){
      this.setData({
        isShowAnswer:'flex',
        isDisabled: true,  //选项按钮不可用
      })
    }else{
      this.setData({
        isShowAnswer:'none',
        isDisabled: false,  //选项按钮可用
      })
    }
    if (index == this.data.questionnum - 1) {
      this.setData({
        isFinish:false  //结束答题按钮可用
      })
    }
  },
  toPrev(){
    this.setData({
      index: this.data.index - 1,
    })
  },
  //跳转下一道题
  toNext() {
      this.setData({
        index: this.data.index + 1,
      })

  },
  //跳转结束页面
  GoEnd: function() {
    wx.showLoading({
      title:'成绩计算中',
      mask:true
    })
    var url = '';
    app.globalData.grade = this.data.grade;
    if (this.data.grade > this.data.topscore) {
      this.data.topscore = this.data.grade;
    }

    if (this.data.grade >= this.data.passscore) {
      this.data.passtimes++;
      url = '../success/success';
    } else {
      url = '../end/end';
    }
    app.globalData.passtimes = this.data.passtimes;
    wx.cloud.callFunction({
      name: "updateUserInfo",
      data: {
        topscore: this.data.topscore,
        passtimes: this.data.passtimes,
        answertimes: this.data.answertimes + 1,
      },
      success: res => {
        console.log("更新完成");
      },
      fail: err => {
        // handle error
        wx.showModal({
          title: '网络错误',
          showCancel: false,
          content: '抱歉，您的成绩将无法保存(＞人＜；',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log(err);
      },
      complete: () => {
        wx.hideLoading()
      }
    })
    wx.redirectTo({
      url: url,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var _this = this;
    this.setData({
      grade: 0,
    })
    // //查询数据  news对应的是集合的名称   
    // db.collection('QandA').get({
    //   //如果查询成功的话    
    //   success: res => {
    //     //这一步很重要，给ne赋值，没有这一步的话，前台就不会显示值      
    //     //console.log(res);
    //     this.setData({
    //       net: res.data
    //     })
    //   }
    // })
    wx.cloud.callFunction({
      name:'getQuestions',
      success:res=>{
        //console.log(res.result.rst.data)
        _this.setData({
          net:res.result.rst.data,
          questionnum:res.result.rst.data.length
        })
      }
    })
    db.collection('userInfo').get({
      success: res => {
        this.setData({
          passtimes: res.data[0].passtimes, //获取当前用户的通过次数、最高成绩和总答题次数
          topscore: res.data[0].topscore,
          answertimes: res.data[0].answertimes,
        });

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})